package com.ilop.sthome.ui.activity.config;

import android.Manifest;
import android.content.ComponentName;
import android.content.Intent;

import com.example.common.base.BaseBindActivity;
import com.example.common.base.OnCallBackToRefresh;
import com.ilop.sthome.common.CommonId;
import com.example.common.utils.PermissionUtil;
import com.ilop.sthome.ui.dialog.BaseDialog;
import com.ilop.sthome.utils.system.AppUtil;
import com.siterwell.familywellplus.R;
import com.siterwell.familywellplus.databinding.ActivitySetApBinding;

/**
 * @author skygge
 * @date 2019-10-28.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：设置手机AP配网
 */
public class SetPhoneApActivity extends BaseBindActivity<ActivitySetApBinding> {

    private String mSid;
    private String mPsw;

    private String[] mPermission = new String[]{
            Manifest.permission.ACCESS_COARSE_LOCATION,
            Manifest.permission.ACCESS_FINE_LOCATION};

    @Override
    protected int getLayoutId() {
        return R.layout.activity_set_ap;
    }

    @Override
    protected void initialize() {
        super.initialize();
        mSid = getIntent().getStringExtra(CommonId.KEY_WIFI_SSID);
        mPsw = getIntent().getStringExtra(CommonId.KEY_WIFI_PWD);
    }

    @Override
    protected void initData() {
        super.initData();
        PermissionUtil.requestPermission(this, mPermission);
        mDBind.btnConfirm.setEnabled(false);
    }

    @Override
    protected void initListener() {
        super.initListener();
        mDBind.ivSetApBack.setOnClickListener(view -> finish());

        mDBind.apOpen.setOnCheckedChangeListener((buttonView, isChecked) -> getWifiApEnabled(isChecked));

        mDBind.btnConfirm.setOnClickListener(view -> {
            Intent intent = new Intent(mContext, ConnectNetActivity.class);
            intent.putExtra(CommonId.KEY_WIFI_SSID, mSid);
            intent.putExtra(CommonId.KEY_WIFI_PWD, mPsw);
            intent.putExtra(CommonId.KEY_WIFI_AP, true);
            startActivity(intent);
            finish();
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        setViewEnable();
    }

    private void getWifiApEnabled(boolean isChecked){
        if (isChecked){
            boolean isOpen = setViewEnable();
            if (!isOpen) showOpenHotPostDialog();
        }else {
            mDBind.btnConfirm.setBackgroundResource(R.drawable.btn_login_empty_bg);
            mDBind.btnConfirm.setEnabled(false);
        }
    }

    private boolean setViewEnable(){
        boolean isOpen = AppUtil.isWifiApOpen(mContext);
        mDBind.apOpen.setChecked(isOpen);
        mDBind.btnConfirm.setEnabled(isOpen);
        mDBind.btnConfirm.setBackgroundResource(isOpen?R.drawable.btn_gradient_bg:R.drawable.btn_login_empty_bg);
        return isOpen;
    }

    private void showOpenHotPostDialog(){
        BaseDialog mDialog = new BaseDialog(mContext, new OnCallBackToRefresh() {
            @Override
            public void onConfirm() {
                Intent intent = new Intent();
                intent.addCategory(Intent.CATEGORY_DEFAULT);
                intent.setAction("android.intent.action.MAIN");
                ComponentName cn = new ComponentName("com.android.settings", "com.android.settings.Settings$TetherSettingsActivity");
                intent.setComponent(cn);
                startActivity(intent);
            }

            @Override
            public void onCancel() {

            }
        });
        mDialog.setTitleAndButton(getString(R.string.the_settings_list_open_personal_hotspots), getString(R.string.cancel), getString(R.string.goto_set));
        mDialog.show();
    }
}
